// 首页
// http://web.wangpengying.com.cn/

// 接单
// http://gjd.api.wangpengying.com.cn/api.php/task/receiving

// 登录
// http://gjd.api.wangpengying.com.cn/api.php/user/login


// token=7565919829911619&uid=173&key=61DE#!C24#!B3CAA33CA707A0A472677



// 登录


function stringToBytes(t) {
    for (var e = [], n = 0; n < t.length; n++)
        e.push(255 & t.charCodeAt(n));
    return e;
}

function bytesToWords(t) {
    for (var e = [], n = 0, r = 0; n < t.length; n++, r += 8)
        e[r >>> 5] |= t[n] << (24 - (r % 32));
    return e;
}

function endian(t) {
    if (t.constructor == Number)
        return (16711935 & n.rotl(t, 8)) | (4278255360 & n.rotl(t, 24));
    for (var e = 0; e < t.length; e++) t[e] = n.endian(t[e]);
    return t;
}

function wordsToBytes(t) {
    for (var e = [], n = 0; n < 32 * t.length; n += 8)
        e.push((t[n >>> 5] >>> (24 - (n % 32))) & 255);
    return e;
}

function bytesToHex(t) {
    for (var e = [], n = 0; n < t.length; n++)
        e.push((t[n] >>> 4).toString(16)),
            e.push((15 & t[n]).toString(16));
    return e.join("");
}

function _ff(t, e, n, r, i, o, a) {
    var s = t + ((e & n) | (~e & r)) + (i >>> 0) + a;
    return ((s << o) | (s >>> (32 - o))) + e;
}

function _gg(t, e, n, r, i, o, a) {
    var s = t + ((e & r) | (n & ~r)) + (i >>> 0) + a;
    return ((s << o) | (s >>> (32 - o))) + e;
}

function _hh(t, e, n, r, i, o, a) {
    var s = t + (e ^ n ^ r) + (i >>> 0) + a;
    return ((s << o) | (s >>> (32 - o))) + e;
}

function _ii(t, e, n, r, i, o, a) {
    var s = t + (n ^ (e | ~r)) + (i >>> 0) + a;
    return ((s << o) | (s >>> (32 - o))) + e;
}

function rotl(t, e) {
    return (t << e) | (t >>> (32 - e));
}

function endian(t) {
    if (t.constructor == Number) return (16711935 & rotl(t, 8)) | (4278255360 & rotl(t, 24));
    for (var e = 0; e < t.length; e++) t[e] = endian(t[e]);
    return t;
}

function sign(t) {
    t = stringToBytes(t)

    for (var s = bytesToWords(t), c = 8 * t.length, u = 1732584193, l = -271733879, d = -1732584194, h = 271733878, f = 0; f < s.length; f++)
        s[f] = (16711935 & ((s[f] << 8) | (s[f] >>> 24))) | (4278255360 & ((s[f] << 24) | (s[f] >>> 8)));
    (s[c >>> 5] |= 128 << c % 32), (s[14 + (((c + 64) >>> 9) << 4)] = c);

    var p = _ff,
        m = _gg,
        v = _hh,
        g = _ii;
    for (f = 0; f < s.length; f += 16) {
        var y = u,
            b = l,
            w = d,
            S = h;
        (u = p(u, l, d, h, s[f + 0], 7, -680876936)),
            (h = p(h, u, l, d, s[f + 1], 12, -389564586)),
            (d = p(d, h, u, l, s[f + 2], 17, 606105819)),
            (l = p(l, d, h, u, s[f + 3], 22, -1044525330)),
            (u = p(u, l, d, h, s[f + 4], 7, -176418897)),
            (h = p(h, u, l, d, s[f + 5], 12, 1200080426)),
            (d = p(d, h, u, l, s[f + 6], 17, -1473231341)),
            (l = p(l, d, h, u, s[f + 7], 22, -45705983)),
            (u = p(u, l, d, h, s[f + 8], 7, 1770035416)),
            (h = p(h, u, l, d, s[f + 9], 12, -1958414417)),
            (d = p(d, h, u, l, s[f + 10], 17, -42063)),
            (l = p(l, d, h, u, s[f + 11], 22, -1990404162)),
            (u = p(u, l, d, h, s[f + 12], 7, 1804603682)),
            (h = p(h, u, l, d, s[f + 13], 12, -40341101)),
            (d = p(d, h, u, l, s[f + 14], 17, -1502002290)),
            (l = p(l, d, h, u, s[f + 15], 22, 1236535329)),
            (u = m(u, l, d, h, s[f + 1], 5, -165796510)),
            (h = m(h, u, l, d, s[f + 6], 9, -1069501632)),
            (d = m(d, h, u, l, s[f + 11], 14, 643717713)),
            (l = m(l, d, h, u, s[f + 0], 20, -373897302)),
            (u = m(u, l, d, h, s[f + 5], 5, -701558691)),
            (h = m(h, u, l, d, s[f + 10], 9, 38016083)),
            (d = m(d, h, u, l, s[f + 15], 14, -660478335)),
            (l = m(l, d, h, u, s[f + 4], 20, -405537848)),
            (u = m(u, l, d, h, s[f + 9], 5, 568446438)),
            (h = m(h, u, l, d, s[f + 14], 9, -1019803690)),
            (d = m(d, h, u, l, s[f + 3], 14, -187363961)),
            (l = m(l, d, h, u, s[f + 8], 20, 1163531501)),
            (u = m(u, l, d, h, s[f + 13], 5, -1444681467)),
            (h = m(h, u, l, d, s[f + 2], 9, -51403784)),
            (d = m(d, h, u, l, s[f + 7], 14, 1735328473)),
            (l = m(l, d, h, u, s[f + 12], 20, -1926607734)),
            (u = v(u, l, d, h, s[f + 5], 4, -378558)),
            (h = v(h, u, l, d, s[f + 8], 11, -2022574463)),
            (d = v(d, h, u, l, s[f + 11], 16, 1839030562)),
            (l = v(l, d, h, u, s[f + 14], 23, -35309556)),
            (u = v(u, l, d, h, s[f + 1], 4, -1530992060)),
            (h = v(h, u, l, d, s[f + 4], 11, 1272893353)),
            (d = v(d, h, u, l, s[f + 7], 16, -155497632)),
            (l = v(l, d, h, u, s[f + 10], 23, -1094730640)),
            (u = v(u, l, d, h, s[f + 13], 4, 681279174)),
            (h = v(h, u, l, d, s[f + 0], 11, -358537222)),
            (d = v(d, h, u, l, s[f + 3], 16, -722521979)),
            (l = v(l, d, h, u, s[f + 6], 23, 76029189)),
            (u = v(u, l, d, h, s[f + 9], 4, -640364487)),
            (h = v(h, u, l, d, s[f + 12], 11, -421815835)),
            (d = v(d, h, u, l, s[f + 15], 16, 530742520)),
            (l = v(l, d, h, u, s[f + 2], 23, -995338651)),
            (u = g(u, l, d, h, s[f + 0], 6, -198630844)),
            (h = g(h, u, l, d, s[f + 7], 10, 1126891415)),
            (d = g(d, h, u, l, s[f + 14], 15, -1416354905)),
            (l = g(l, d, h, u, s[f + 5], 21, -57434055)),
            (u = g(u, l, d, h, s[f + 12], 6, 1700485571)),
            (h = g(h, u, l, d, s[f + 3], 10, -1894986606)),
            (d = g(d, h, u, l, s[f + 10], 15, -1051523)),
            (l = g(l, d, h, u, s[f + 1], 21, -2054922799)),
            (u = g(u, l, d, h, s[f + 8], 6, 1873313359)),
            (h = g(h, u, l, d, s[f + 15], 10, -30611744)),
            (d = g(d, h, u, l, s[f + 6], 15, -1560198380)),
            (l = g(l, d, h, u, s[f + 13], 21, 1309151649)),
            (u = g(u, l, d, h, s[f + 4], 6, -145523070)),
            (h = g(h, u, l, d, s[f + 11], 10, -1120210379)),
            (d = g(d, h, u, l, s[f + 2], 15, 718787259)),
            (l = g(l, d, h, u, s[f + 9], 21, -343485551)),
            (u = (u + y) >>> 0),
            (l = (l + b) >>> 0),
            (d = (d + w) >>> 0),
            (h = (h + S) >>> 0);
    }
    return endian([u, l, d, h]);
};

function signFun() {
    let phone = parameter.getMap("username")
    let password = parameter.getMap("password")
    let url = parameter.getMap("url")

    let t = 'phone=' + phone + '&pwd=' + password + '&type=0&key=61DE#!C24#!B3CAA33CA707A0A472677'
    let s = sign(t)
    s = wordsToBytes(s)
    s = bytesToHex(s)
    s = s.toUpperCase()
    let body = {
        "phone": phone,
        "type": 0,
        "pwd": password,
        "sign": s
    }
    let headers = {}

    return {
        mediaType: "json",
        requestType: "POST",
        url: url,
        headers: JSON.stringify(headers),
        body: JSON.stringify(body)
    }
}
signFun()

// 处理返回数据
function getResponse() {
    let res = response.getMap("res")
    res = JSON.parse(res)

    let code = 500
    let data = {}
    let params = {}
    if (res['code'] == 1) {
        code = 200
        data = res['data']
    }
    let message = res['msg']
    return {
        code: code,
        message: message,
        token: JSON.stringify(data),
        params: JSON.stringify(params)
    }
}
getResponse()



// 接单

function stringToBytes(t) {
    for (var e = [], n = 0; n < t.length; n++)
        e.push(255 & t.charCodeAt(n));
    return e;
}

function bytesToWords(t) {
    for (var e = [], n = 0, r = 0; n < t.length; n++, r += 8)
        e[r >>> 5] |= t[n] << (24 - (r % 32));
    return e;
}

function endian(t) {
    if (t.constructor == Number)
        return (16711935 & n.rotl(t, 8)) | (4278255360 & n.rotl(t, 24));
    for (var e = 0; e < t.length; e++) t[e] = n.endian(t[e]);
    return t;
}

function wordsToBytes(t) {
    for (var e = [], n = 0; n < 32 * t.length; n += 8)
        e.push((t[n >>> 5] >>> (24 - (n % 32))) & 255);
    return e;
}

function bytesToHex(t) {
    for (var e = [], n = 0; n < t.length; n++)
        e.push((t[n] >>> 4).toString(16)),
            e.push((15 & t[n]).toString(16));
    return e.join("");
}

function _ff(t, e, n, r, i, o, a) {
    var s = t + ((e & n) | (~e & r)) + (i >>> 0) + a;
    return ((s << o) | (s >>> (32 - o))) + e;
}

function _gg(t, e, n, r, i, o, a) {
    var s = t + ((e & r) | (n & ~r)) + (i >>> 0) + a;
    return ((s << o) | (s >>> (32 - o))) + e;
}

function _hh(t, e, n, r, i, o, a) {
    var s = t + (e ^ n ^ r) + (i >>> 0) + a;
    return ((s << o) | (s >>> (32 - o))) + e;
}

function _ii(t, e, n, r, i, o, a) {
    var s = t + (n ^ (e | ~r)) + (i >>> 0) + a;
    return ((s << o) | (s >>> (32 - o))) + e;
}

function rotl(t, e) {
    return (t << e) | (t >>> (32 - e));
}

function endian(t) {
    if (t.constructor == Number) return (16711935 & rotl(t, 8)) | (4278255360 & rotl(t, 24));
    for (var e = 0; e < t.length; e++) t[e] = endian(t[e]);
    return t;
}

function sign(t) {
    t = stringToBytes(t)

    for (var s = bytesToWords(t), c = 8 * t.length, u = 1732584193, l = -271733879, d = -1732584194, h = 271733878, f = 0; f < s.length; f++)
        s[f] = (16711935 & ((s[f] << 8) | (s[f] >>> 24))) | (4278255360 & ((s[f] << 24) | (s[f] >>> 8)));
    (s[c >>> 5] |= 128 << c % 32), (s[14 + (((c + 64) >>> 9) << 4)] = c);

    var p = _ff,
        m = _gg,
        v = _hh,
        g = _ii;
    for (f = 0; f < s.length; f += 16) {
        var y = u,
            b = l,
            w = d,
            S = h;
        (u = p(u, l, d, h, s[f + 0], 7, -680876936)),
            (h = p(h, u, l, d, s[f + 1], 12, -389564586)),
            (d = p(d, h, u, l, s[f + 2], 17, 606105819)),
            (l = p(l, d, h, u, s[f + 3], 22, -1044525330)),
            (u = p(u, l, d, h, s[f + 4], 7, -176418897)),
            (h = p(h, u, l, d, s[f + 5], 12, 1200080426)),
            (d = p(d, h, u, l, s[f + 6], 17, -1473231341)),
            (l = p(l, d, h, u, s[f + 7], 22, -45705983)),
            (u = p(u, l, d, h, s[f + 8], 7, 1770035416)),
            (h = p(h, u, l, d, s[f + 9], 12, -1958414417)),
            (d = p(d, h, u, l, s[f + 10], 17, -42063)),
            (l = p(l, d, h, u, s[f + 11], 22, -1990404162)),
            (u = p(u, l, d, h, s[f + 12], 7, 1804603682)),
            (h = p(h, u, l, d, s[f + 13], 12, -40341101)),
            (d = p(d, h, u, l, s[f + 14], 17, -1502002290)),
            (l = p(l, d, h, u, s[f + 15], 22, 1236535329)),
            (u = m(u, l, d, h, s[f + 1], 5, -165796510)),
            (h = m(h, u, l, d, s[f + 6], 9, -1069501632)),
            (d = m(d, h, u, l, s[f + 11], 14, 643717713)),
            (l = m(l, d, h, u, s[f + 0], 20, -373897302)),
            (u = m(u, l, d, h, s[f + 5], 5, -701558691)),
            (h = m(h, u, l, d, s[f + 10], 9, 38016083)),
            (d = m(d, h, u, l, s[f + 15], 14, -660478335)),
            (l = m(l, d, h, u, s[f + 4], 20, -405537848)),
            (u = m(u, l, d, h, s[f + 9], 5, 568446438)),
            (h = m(h, u, l, d, s[f + 14], 9, -1019803690)),
            (d = m(d, h, u, l, s[f + 3], 14, -187363961)),
            (l = m(l, d, h, u, s[f + 8], 20, 1163531501)),
            (u = m(u, l, d, h, s[f + 13], 5, -1444681467)),
            (h = m(h, u, l, d, s[f + 2], 9, -51403784)),
            (d = m(d, h, u, l, s[f + 7], 14, 1735328473)),
            (l = m(l, d, h, u, s[f + 12], 20, -1926607734)),
            (u = v(u, l, d, h, s[f + 5], 4, -378558)),
            (h = v(h, u, l, d, s[f + 8], 11, -2022574463)),
            (d = v(d, h, u, l, s[f + 11], 16, 1839030562)),
            (l = v(l, d, h, u, s[f + 14], 23, -35309556)),
            (u = v(u, l, d, h, s[f + 1], 4, -1530992060)),
            (h = v(h, u, l, d, s[f + 4], 11, 1272893353)),
            (d = v(d, h, u, l, s[f + 7], 16, -155497632)),
            (l = v(l, d, h, u, s[f + 10], 23, -1094730640)),
            (u = v(u, l, d, h, s[f + 13], 4, 681279174)),
            (h = v(h, u, l, d, s[f + 0], 11, -358537222)),
            (d = v(d, h, u, l, s[f + 3], 16, -722521979)),
            (l = v(l, d, h, u, s[f + 6], 23, 76029189)),
            (u = v(u, l, d, h, s[f + 9], 4, -640364487)),
            (h = v(h, u, l, d, s[f + 12], 11, -421815835)),
            (d = v(d, h, u, l, s[f + 15], 16, 530742520)),
            (l = v(l, d, h, u, s[f + 2], 23, -995338651)),
            (u = g(u, l, d, h, s[f + 0], 6, -198630844)),
            (h = g(h, u, l, d, s[f + 7], 10, 1126891415)),
            (d = g(d, h, u, l, s[f + 14], 15, -1416354905)),
            (l = g(l, d, h, u, s[f + 5], 21, -57434055)),
            (u = g(u, l, d, h, s[f + 12], 6, 1700485571)),
            (h = g(h, u, l, d, s[f + 3], 10, -1894986606)),
            (d = g(d, h, u, l, s[f + 10], 15, -1051523)),
            (l = g(l, d, h, u, s[f + 1], 21, -2054922799)),
            (u = g(u, l, d, h, s[f + 8], 6, 1873313359)),
            (h = g(h, u, l, d, s[f + 15], 10, -30611744)),
            (d = g(d, h, u, l, s[f + 6], 15, -1560198380)),
            (l = g(l, d, h, u, s[f + 13], 21, 1309151649)),
            (u = g(u, l, d, h, s[f + 4], 6, -145523070)),
            (h = g(h, u, l, d, s[f + 11], 10, -1120210379)),
            (d = g(d, h, u, l, s[f + 2], 15, 718787259)),
            (l = g(l, d, h, u, s[f + 9], 21, -343485551)),
            (u = (u + y) >>> 0),
            (l = (l + b) >>> 0),
            (d = (d + w) >>> 0),
            (h = (h + S) >>> 0);
    }
    return endian([u, l, d, h]);
};

function getParameterHandler() {
    let data = parameter.getMap("token")
    let params = parameter.getMap("params")
    let url = parameter.getMap("url")

    data = JSON.parse(data)
    let uid = data["uid"]
    let token = data["token"]

    let t = 'token=' + token + '&uid=' + uid + '&key=61DE#!C24#!B3CAA33CA707A0A472677'
    let s = sign(t)
    s = wordsToBytes(s)
    s = bytesToHex(s)
    s = s.toUpperCase()
    let body = {
        "uid": uid,
        "token": token,
        "sign": s
    }
    let headers = {}

    return {
        mediaType: "json",
        requestType: "POST",
        url: url,
        headers: JSON.stringify(headers),
        body: JSON.stringify(body)
    }
}
getParameterHandler()


function getResponse() {
    let res = response.getMap("res")
    let code = "300"
    let message = "接单中"
    let dataKey = ""
    let dataValue = ""

    res = JSON.parse(res)
    message = res["msg"]
    if (res["code"] == 1) {
        code = "200"
    } else if (res["code"] == 2) {
        code = "500"
    } else {
        code = "300"
    }

    return {
        code: code,
        message: message,
        dataKey: dataKey,
        dataValue: dataValue
    }
}
getResponse()

